﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace TB_WS.Class
{
    public class Seller_Manage
    {
        /// <summary>
        /// 根据卖家昵称查询数据库中是否存在
        /// </summary>
        /// <param name="Seller_Nick">卖家昵称</param>
        /// <returns></returns>
        public static int get_seller_count(string Seller_Nick)
        {
            string sql = "select count(*) from Seller_Info where Seller_Nick='" + Seller_Nick + "'";
            return Convert.ToInt32(Unit.db_Conn.scalar(sql));
        }

        /// <summary>
        /// 新增商家信息，此处数据不做校验，校验请从客户端
        /// </summary>
        /// <param name="Seller_Nick">商家名称</param>
        /// <param name="Seller_PWD">商家管理密码</param>
        /// <param name="WeChat_AppID">微信公众号AppID</param>
        /// <param name="WeChat_AppSecret">微信公众号AppSecret</param>
        /// <param name="TB_AppKey">淘宝AppKey</param>
        /// <param name="TB_AppSecret">淘宝AppSecret</param>
        /// <param name="TB_Session">淘宝Session</param>
        /// <param name="Last_TB_Session_Time">最后一次更新Session的Unix时间</param>
        /// <returns></returns>
        public static int insert_Seller(string Seller_Nick,string Seller_PWD,
            string WeChat_AppID,string WeChat_AppSecret,
            string TB_AppKey,string TB_AppSecret,
            string TB_Session,string Last_TB_Session_Time)
        {
            string sql = "insert into Seller_Info values('" + Seller_Nick + "','" +
                Seller_PWD + "','" + WeChat_AppID + "','" + WeChat_AppSecret + "','" +
                TB_AppKey + "'" + TB_AppSecret + "','" + TB_Session + "','" + Last_TB_Session_Time + "')";
            return Unit.db_Conn.Nonquery(sql);
        }


        /// <summary>
        /// 根据商家名称获取商家详细信息
        /// </summary>
        /// <param name="seller_Nick">商家名称</param>
        /// <returns></returns>
        public static DataSet select_Seller_Info(string seller_Nick)
        {
            string sql = "select * from Seller_Info where Seller_Nick='" + seller_Nick + "'";
            return Unit.db_Conn.select(sql);
        }
        /// <summary>
        /// 根据商家id获取商家详细信息
        /// </summary>
        /// <param name="seller_Nick">商家名称</param>
        /// <returns></returns>
        public static DataSet select_Seller_Info(int Seller_ID)
        {
            string sql = "select * from Seller_Info where id=" + Seller_ID;
            return Unit.db_Conn.select(sql);
        }

        /// <summary>
        /// 根据用户名和密码校验登录
        /// 返回值 成功：1    失败：0
        /// </summary>
        /// <param name="Seller_Nick">商家名称</param>
        /// <param name="Seller_PWD">密码（请从客户端做好MD5校验）</param>
        /// <returns></returns>
        public static int Login_Conform(string Seller_Nick,string Seller_PWD)
        {
            string sql = "select count(*) from Seller_Info where Seller_Nick='" + Seller_Nick + "' and Seller_PWD='" + Seller_PWD + "'";
            return Convert.ToInt32(Unit.db_Conn.scalar(sql));
        }


        /// <summary>
        /// 根据商家名称 修改商家密码
        /// </summary>
        /// <param name="Seller_Nick">需要修改密码的商家名称</param>
        /// <param name="Seller_PWD">新的商家密码（MD5） 请从客户端生成MD5提交</param>
        /// <returns></returns>
        public static int update_seller_PWD(string Seller_Nick, string Seller_PWD)
        {
            string sql = "update Seller_Info set Seller_PWD='" + Seller_PWD + "' where Seller_Nick='" + Seller_Nick + "'";
            return Unit.db_Conn.Nonquery(sql);
        }


        /// <summary>
        /// 修改商家的微信公众号信息
        /// </summary>
        /// <param name="Seller_Nick">商家名称</param>
        /// <param name="WeChat_AppID">微信公众号AppID</param>
        /// <param name="WeChat_AppSecret">微信公众号AppSecret</param>
        /// <returns></returns>
        public static int update_seller_WeChat_Info(string Seller_Nick,
            string WeChat_AppID, string WeChat_AppSecret)
        {
            string sql = "update Seller_Info set WeChat_AppID='" +
                WeChat_AppID + "',WeChat_AppSecret='" +
                WeChat_AppSecret + "' where Seller_Nick='" + Seller_Nick + "'";
            return Unit.db_Conn.Nonquery(sql);
        }

        /// <summary>
        /// 修改商家淘宝开放平台信息
        /// </summary>
        /// <param name="Seller_Nick">商家昵称</param>
        /// <param name="TB_AppKey">淘宝开放平台AppKey</param>
        /// <param name="TB_AppSecret">淘宝开放平台AppSecret</param>
        /// <returns></returns>
        public static int update_seller_TB_Info(string Seller_Nick, 
            string TB_AppKey, string TB_AppSecret)
        {
            string sql = "update Seller_Info set TB_AppKey='" + TB_AppKey +
                "',TB_AppSecret='" + TB_AppSecret + "' where Seller_Nick='" +
                Seller_Nick + "'";
            return Unit.db_Conn.Nonquery(sql);
        }

        /// <summary>
        /// 更新商家淘宝授权Session信息
        /// </summary>
        /// <param name="Seller_Nick">商家昵称</param>
        /// <param name="TB_Session">淘宝开放平台Session信息</param>
        /// <returns></returns>
        public static int update_Seller_Session(string Seller_Nick,string TB_Session)
        {
            string sql = "update Seller_Info set TB_Session='" +
                TB_Session + "',Last_TB_Session_Time='" +
                LeShiTao_Library.Tools.Data_Handle.GET_UNIX_TIMESTAMP().ToString() + 
                "' where Seller_Nick='" + Seller_Nick + "'";
            return Unit.db_Conn.Nonquery(sql);
        }
    }
}
